<?php

namespace App\Controller\LazyCommon;

use App\Base\BaseConfig;
use App\Base\BaseController4User;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\UserInviteModel;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
 * @Route("/web")
 */
class WebController extends BaseController4User
{
    /**
     * @Route("/download",name="WebAppDownLoad")
     */
    public function WebAppDownLoad()
    {
        $user = $this->getUser();
        $data["rnd"] = "?rnd=".date("YmdHi");
        $data["root"] = BaseConfig::__BASE_URL__;
        $data["id"] = 0;
        if ($user) {
            $data["id"] = $user["id"];
        }
        return $this->render("App:Web:download.html.twig",$data);
    }

    /**
     * @Route("/reg/page",name="WebUserRegPage")
     */
    public function WebUserRegPage()
    {
        if(!$code = $this->getParamRequired("code")){$code = "";}
        $data["code"] = $code;

        $data["rnd"] = "?rnd=".date("YmdHi");
        $data["root"] = BaseConfig::__BASE_URL__;
        return $this->render("App:Web:reg.html.twig",$data);
    }



    /**
     * @Route("/reg/do",name="WebUserRegDo")
     */
    public function UserReg()
    {
        if(!$phone = $this->getParamRequired("phone")){return $this->msg_out("请设置手机号");}
        if(!$password = $this->getParamRequired("password")){return $this->msg_out("请设置密码");}
        if(!$code = $this->getParamRequired("code")){$code = "";}


        $UserModel = new UserModel($this->getDoctrine());
        if ($UserModel->checkAccount4Reg($phone)) {return $this->json_out(0,"手机号已注册","1");}
        $password = md5($password);
        $userInfo["account"] = $phone;
        $userInfo["password"] = $password;
        $userInfo["passwordPay"] = $password;
        $user = $UserModel->commReg($userInfo);
        $sessionData["user"] = array(
            "id"    => $user->getId(),
            "phone" => $user->getAccount()
        );
        $this->setSessionData($sessionData);

        //添加注册人绑定
        $UserInviteModel = new UserInviteModel($this->getDoctrine());
        if ($code) {
            $UserInviteModel->addUser($user->getId(),$code);
        }
        return $this->redirectToRoute("WebAppDownLoad");

    }
}
